package com.bit.demo.demo1;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/request")
public class RequestController {
    @RequestMapping("login")
    public String login(String name) {
        return "接受到参数 ：" + name ;
    }

    @RequestMapping("login2")
    public String login2(@RequestParam("QQ") String name,@RequestParam("WEI") String age) {
        return "接收到参数 : " + name + ", " + age;
    }

    @RequestMapping("penple1")
    public String People1(Penple penple) {
        return penple.toString();
    }

    @RequestMapping("time1")
    public String time(@RequestParam(value = "qq", required = false) String createtime) {
        return "创建时间：" + createtime;
    }

    @RequestMapping("Array1")
    public String Array(String[] array) {
        return "数组：" + Arrays.toString(array) + ", " + "数组大小 ：" + array.length;
    }

    @RequestMapping("List1")
    public String List1(@RequestParam List<String> list) {
        return "列表：" + list.toString();
    }

    @RequestMapping("Json1")
    public String JsonDemo(@RequestBody Penple penple) {
        return "Json数据：" + penple;
    }

    @RequestMapping(" Json2/{id}/{name}")
    public String Json2(@PathVariable int id,@PathVariable("name") String usename) {
        return "数据id :" + id + ", 数据name :" + usename;
    }

    @RequestMapping("file")
    public String file(@RequestPart("file") MultipartFile unfile) throws IOException {
        //获取文件名称
        String filename = unfile.getOriginalFilename();
        //上传
        unfile.transferTo(new File("D:/GAO/" + filename));
        return "上传成功" + filename;
    }

    @RequestMapping("getCookie")
    public String getCookie(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        StringBuilder builder = new StringBuilder();
        if (cookies != null) {
            for(Cookie ck : cookies) {
                builder.append(ck.getName() + " : " + ck.getValue());
            }
        }
        return "Cookie信息 :" + builder;
    }

    @RequestMapping("getCookie1")
    public String getCookie1(@CookieValue("bite") String value) {
        return "bite :" + value;
    }

    @RequestMapping("setSess")
    public String setSess(HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session != null) {
            session.setAttribute("name","zhangsan666");
        }
        return "设置成功";
    }

    @RequestMapping("getSess")
    public String getSess(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        String name = null;
        if (session != null && session.getAttribute("name") != null) {
            name = (String)session.getAttribute("name");
        }
        return "name: " + name;
    }

    @RequestMapping("getSess1")
    public String getSess1(@SessionAttribute(value = "name" , required = false) String name){
        return "name" + name;
    }

    @RequestMapping("getSess2")
    public String getSess2(HttpSession session) {
        String name = (String)session.getAttribute("name");
        return "name" + name;
    }

    @RequestMapping("getHead")
    public String getHead(HttpServletRequest request) {
        String Header = request.getHeader("User-Agent");
        return "请求头: " + Header;
    }

    @RequestMapping("getHead2")
    public String getHead1(@RequestHeader("User-Agent") String name){
        return "请求头: " + name;
    }



}
